package com.czn.service.impl;

import com.czn.dao.MedicalhistoryDao;
import com.czn.entity.Medicalhistory;
import com.czn.service.IMedicalHistoryService;
import com.czn.vo.QueryVo;
import com.czn.vo.ResponseResult;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author ChenZeNan
 * @create 2021-04-23 20:29
 */
@Service
public class MedicalHistoryImpl implements IMedicalHistoryService {
    @Autowired
    private MedicalhistoryDao medicalhistoryDao;

    /**
     * 分页/模糊查询病史
     *
     * @param queryVo
     * @return
     */
    @Override
    public PageQuery<Medicalhistory> findMedicalHistoryList(QueryVo queryVo) {
        PageQuery<Medicalhistory> query = new PageQuery<>(queryVo.getPageNo(), queryVo.getPageSize());
        if (!StringUtils.isEmpty(queryVo.getPatientName())) {
            query.setPara("patientName", '%' + queryVo.getPatientName() + '%');
        }
        if (!StringUtils.isEmpty(queryVo.getDoctorName())) {
            query.setPara("doctorName", '%' + queryVo.getDoctorName() + '%');
        }
        if (queryVo.getUserId() != null) {
            query.setPara("userId", queryVo.getUserId());
        }
        if (queryVo.getPatientId() != null) {
            query.setPara("patientId", queryVo.getPatientId());
        }
        medicalhistoryDao.selectLit(query);
        return query;
    }

    /**
     * 根据id查询病史
     *
     * @param id
     * @return
     */
    @Override
    public Medicalhistory findMedicalHistoryById(Integer id) {
        Medicalhistory medicalhistory = medicalhistoryDao.selectOne(id);
        return medicalhistory;
    }

    /**
     * 添加病史
     *
     * @param medicalhistory
     * @return
     */
    @Override
    public ResponseResult addMedicalHistory(Medicalhistory medicalhistory) {
        ResponseResult result = new ResponseResult();
        medicalhistoryDao.insert(medicalhistory);
        result.setCode("300");
        result.setMessage("信息添加成功！");
        return result;
    }

    /**
     * 修改病史
     *
     * @param medicalhistory
     * @return
     */
    @Override
    public ResponseResult editMedicalHistory(Medicalhistory medicalhistory) {
        ResponseResult result = new ResponseResult();
        medicalhistoryDao.updateTemplateById(medicalhistory);
        result.setCode("500");
        result.setMessage("信息更新成功！");
        return result;
    }

    /**
     * 删除病史
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteMedicalHistoryById(Integer id) {
        ResponseResult result = new ResponseResult();
        int rows = medicalhistoryDao.deleteById(id);
        if (rows > 0) {
            result.setCode("400");
            result.setMessage("删除成功！");
            return result;
        }
        result.setCode("401");
        result.setMessage("删除失败！");
        return result;
    }
}
